<?php

class Application_Model_CategoryService extends Base_Service
{
   
	public function create($category) {		
		$db = $this->getDbAdapter();
		
		$row = array ( 
		'category' => $category
		);
		$this->logger->debug(__METHOD__. '   ' .$row);		
		$rows_affected = $db->insert('category', $row);
		$last_insert_id = $db->lastInsertId();
		$this->logger->debug(__METHOD__. '   ' .$last_insert_id);
		
		return $last_insert_id;
		
	}
	
	
 	public function getCategoryId($category)
	{
		$table = new Application_Model_DbTable_Category();
		$resultSet = $table-> fetchAll("category='".$category."'",null,1);
		
		foreach ($resultSet as $row) {
			return $row->cat_id;			
		}
		return -1;
	}
	/**
	 * Not used tags will not be selected
	 */
	public function getAllCategories($uid){
		$this->logger->debug(__METHOD__."  get all categories with uid: ".$uid);
		$db = $this->getDbAdapter();
		if ($uid) {
			return $db->fetchAll("select a.*, count(a.cat_id) count"
			." from category a, blog_category b, blog c"
			." where a.cat_id = b.cat_id and b.bid=c.bid and c.owner_id=".$uid
			." group by a.cat_id order by a.category");
		} else {
			return $db->fetchAll("select a.*, count(a.cat_id) count from category a, blog_category b where a.cat_id = b.cat_id group by a.cat_id order by a.category");
		}
	}
	
	public function getCategory($catId)
	{
		if ($catId) {
			$table = new Application_Model_DbTable_Category();
			$resultSet = $table-> fetchAll("cat_id='".$catId."'",null,1);
			
			foreach ($resultSet as $row) {
				return $row->category;			
			}
		}	
	}
	protected static $_instance = null;
	public static function getInstance() {
        if (null === self::$_instance) {
            self::$_instance = new self();
        }

        return self::$_instance;
    }
}

